<?php

class Pageley_Reflection_InvocationHandlerMock implements Pageley_Reflection_InvocationHandler {
    private $recordedInvocations;
    private $recording;

    public function __construct() {
        $this->recordedInvocations = new Pageley_Util_ArrayMap();
        $this->recording = true;
    }
    
    public function invoke($proxy, $methodName, $parameters) {
        $invocationKey = get_class($proxy) . "." . $methodName . "(" . $parameters . ")";
        if($this->recording) {
            $recording = new Pageley_Reflection_Recording($invocationKey);
            $this->recordedInvocations->put($invocationKey, $recording);
            return $recording;
        } else {
            $recording = $this->recordedInvocations->shift();
            if($recording->getKey() != $invocationKey) {
                throw new Pageley_Test_AssertionException("Expected <" . $expectedKey . ">, found <" . $invocationKey . ">.");
            }
            return $recording->executeAndReturn($parameters);
        }
        
    }
    
    public function replay() {
        $this->recording = false;
    }
    
    public function record() {
        $recordedInvocations->clear();
        $this->recording = true;
    }
}